<template>
  <el-form label-width="100px" v-show="visible">
    <el-form-item label="SPU 名称">
      <span>{{spuName}}</span>
    </el-form-item>

    <el-form-item label="SKU 名称">
      <el-input type="text" placeholder="SKU 名称" v-model="skuInfo.skuName"></el-input>
    </el-form-item>

    <el-form-item label="价格(元)">
      <el-input type="number" placeholder="SKU 价格" v-model="skuInfo.price"></el-input>
    </el-form-item>

    <el-form-item label="重量(千克)">
      <el-input type="number" placeholder="SKU 重量" v-model="skuInfo.weight"></el-input>
    </el-form-item>

    <el-form-item label="规格描述">
      <el-input type="textarea" placeholder="SKU 规格描述" rows="4" v-model="skuInfo.skuDesc"></el-input>
    </el-form-item>

    <el-form-item label="平台属性">
      <el-form inline label-width="100px">
        <el-form-item :label="attr.attrName" style="margin: 5px 0"
                      v-for="attr in skuAttrValueList" :key="attr.id">
          <el-select  placeholder="请输入" v-model="attr.attrIDAndValID">
            <el-option :label="value.valueName" :value="`${attr.id}-${value.id}`"
                       v-for="value in attr.attrValueList" :key="value.id"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="销售属性">
      <el-form inline label-width="100px">
        <el-form-item :label="saleAttr.saleAttrName" style="margin: 5px 0"
                      v-for="saleAttr in skuSaleAttrValueList" :key="saleAttr.id">
          <el-select  placeholder="请输入" v-model="saleAttr.saleAttrValueID">
            <el-option :label="value.saleAttrValueName" :value="value.id"
              v-for="value in saleAttr.spuSaleAttrValueList" :key="value.id"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="图片列表">
      <el-table border :data="skuImageList" @selection-change="imageChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="图片">
          <template slot-scope="{row}">
            <img :src="row.imgUrl" alt="" style="width:100px;height:100px;">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{row, $index}">
            <el-button type="primary" size="small"
                       @click="setDefault($index)"
                       v-if="row.isDefault!==`1`">设为默认</el-button>
            <el-tag type="info" :disable-transitions="true" v-else>默认</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  export default {
    name: 'addSku',
    props:["visible"],
    data(){
      return {
        spuName:"",
        skuInfo:{
          skuName:"",//sku名称
          skuDesc:"",//sku描述
          weight: "",//重量
          price: "", //价格

          category3Id: "",//三级分类的id
          spuId: "",  //spu的id
          tmId: "", //品牌id


          skuAttrValueList: [], //收集平台属性
          skuSaleAttrValueList: [],//收集销售属性

          skuDefaultImg: "",  //收集默认图片
          skuImageList: []  ////收集图片列表
        },

        skuAttrValueList: [], //显示所有的平台属性
        skuImageList: [],  ////显示所有的图片列表
        skuSaleAttrValueList: [],//显示所有的销售属性
        selectedImageList:[] //选中的图片
      }
    },
    methods:{
      //保存按钮的逻辑
      async save(){
         let {skuInfo} = this;
         //收集平台属性
         this.skuAttrValueList.forEach((item)=>{
            if(item.attrIDAndValID){
              const [attrId,valueId] =  item.attrIDAndValID.split("-");
              skuInfo.skuAttrValueList.push( {
                attrId,
                valueId
              })
            }
          })
         //收集销售属性
         this.skuSaleAttrValueList.forEach((item)=>{
          if(item.saleAttrValueID){
            const saleAttrValueId = item.saleAttrValueID
            skuInfo.skuSaleAttrValueList.push({
              saleAttrValueId
            })
          }
        })
         //收集图片列表
         this.skuInfo.skuImageList = this.selectedImageList;

         //发送请求
         await this.$api.product.sku.addSKU(this.skuInfo)
         //给一个成功的提醒
         this.$message.success("新增sku成功")
         //关闭当前的新增sku的面板
         this.cancel()
         //刷新界面
         this.$emit("success")
      },

      //当选中的图片发生改变时触发
      imageChange(imgList){
        //imgList就是外部的skuImageList
        //引用数据类型的传递
        this.selectedImageList = imgList;
      },

      //设置默认图片
      setDefault(currentIndex){
        //外部的skuImageList 动这个skuImageList 会影响界面的渲染
        this.skuImageList.forEach((image,index)=>{
          //把所有图片的isDefault置为空
          this.$set(image,"isDefault","0")
          //把选中的那一张置为`1`
          if(currentIndex === index){
            this.$set(image,"isDefault","1");
            this.skuInfo.skuDefaultImg = image.imgUrl;
          }
        })
      },

      //初始化数据
      async initData(spuInfo){
        //spu名称
        this.spuName = spuInfo.spuName;

        //为请求准备数据  这些数据当前页面没有提供
        this.skuInfo.category3Id= spuInfo.category3Id;//三级分类的id
        this.skuInfo.spuId= spuInfo.id;  //spu的id
        this.skuInfo.tmId= spuInfo.tmId;//品牌id


        //获取当前界面的基础信息
        const {category1Id,category2Id,category3Id} = spuInfo;
        //发送请求获取 所有的平台属性 所有的图片列表 所有的销售属性
        const getAttrList = this.$api.product.attr.getAttrInfoList({category1Id,category2Id,category3Id})
        const getImageList = this.$api.product.spu.getSpuImageList(spuInfo.id)
        const getSaleAttrList = this.$api.product.spu.getSpuSaleAttrList(spuInfo.id);

        const [{code:attrCode,data:attrList },
          {code:imageCode,data:imageList},
          {code:saleAttrCode,data:saleAttrList }] = await Promise.all([getAttrList,getImageList,getSaleAttrList]);

        if(attrCode===200&imageCode===200&saleAttrCode===200){
          this.skuAttrValueList = attrList;
          this.skuImageList = imageList;
          this.skuSaleAttrValueList = saleAttrList;
        }
      },

      //取消按钮的逻辑
      cancel(){
        this.$emit("update:visible",false)
      }
    }
  }
</script>
